package com.huike.clues.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.SysDictData;
import com.huike.clues.domain.SysDictType;
import com.huike.clues.domain.dto.SystemDictTypeDto;
import com.huike.clues.domain.dto.SystemDictDataDto;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.service.ISysDictService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictDataDTO;
import com.huike.common.core.domain.entity.SysDictTypeDTO;
import com.huike.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictType> implements ISysDictService {
     @Autowired
     private ISysDictService sysDictService;

     @Autowired
     private RedisTemplate redisTemplate;
     @Autowired
      private SysDictDataMapper sysDictDataMapper;

    /**
     * 定义缓存常量存储区域
     */
    private static final String DICT_KEY = "sys_dict_type:";
//    @Override
//    public void addType(SysDictDataDto dictData) {
//        SysDictType dictType = BeanUtil.toBean(dictData, SysDictType.class);
//        this.save(dictType);
//    }

    /**
     * 修改字典类型
     *
     * @param dto
     */
    @Override
    public Long updateType(SystemDictTypeDto dto) {
        SysDictType sysDictType = BeanUtil.toBean(dto, SysDictType.class);

        boolean flag = this.update(sysDictType, Wrappers.<SysDictType>lambdaQuery().eq(SysDictType::getDictName,sysDictType.getDictName()));
        return flag ? 1:0L;
    }

    /**
     * 清空缓存
     */
    @Override
    public void clearCache() {
       redisTemplate.delete(DICT_KEY);

    }

    /**
     * 导出数据（写的有问题）
     *
     * @param dictType
     */
    @Override
    public  List< SysDictType> selectDictList(SysDictType dictType) {
        List<SysDictType> list = sysDictService.list();
        SysDictTypeDTO  dictTypeDTO = BeanUtil.toBean(list, SysDictTypeDTO.class);
        ExcelUtil<SysDictTypeDTO> dtoExcelUtil= new ExcelUtil<SysDictTypeDTO>(SysDictTypeDTO.class);


        return null;
    }

    /**
     * 分页查询字典数据
     *
     * @param dictType
     * @return
     */
    @Override
    public List<SysDictTypeDTO> selectDictDataList(SysDictTypeDTO dictType) {


      List<SysDictTypeDTO> list= sysDictDataMapper.selectDictList(dictType);

      return list;

    }

    /**
     * 获取字典选择框列表
     *
     * @return
     */
    @Override
    public List<SysDictType> selectDictTypeAll() {
        return sysDictDataMapper.selectDictTypeAll();
    }

    /**
     * 删除字典
     */
    @Override
    public int removeid(String[] dictIds) {
        boolean flag = this.removeByIds(Arrays.asList(dictIds));
        redisTemplate.delete(DICT_KEY);
        return flag ? 1:0;

    }

    /**
     * 新增字典数据
     *
     * @param dictData
     * @return
     */
    @Override
    public String addData(SysDictDataDTO dictData) {
        SysDictData sysDictData = BeanUtil.toBean(dictData, SysDictData.class);
     //
        return null;
    }


}
